/**
 * desc：
 * author：王文军
 * date：  2022/4/9 17:31
 */
"use strict";
const config = require("../../config/db");

class OSS {
  constructor(bucket) {
    this.oss = require("ali-oss")({
      accessKeyId: config.OSS_ACCESS_KEY,
      accessKeySecret: config.OSS_ACCESS_SECRET_KEY,
      bucket,
      region: config.OSS_REGION,
    });
  }

  /**
   * 查询OSS项目中文件列表
   * @param {string} prefix 项目名称
   * @return {Promise<*[]|*>} array
   */
  async list(prefix) {
    const ossFileList = await this.oss.list({
      prefix,
    });
    if (ossFileList && ossFileList.objects) {
      return ossFileList.objects;
    }
    return [];
  }

  async put(object, localPath, options = {}) {
    await this.oss.put(object, localPath, options);
  }
}

module.exports = OSS;
